﻿using project.B12.IRepository.Base;
using project.B12.IRepository;
using project.B12.Model.Models;
using project.B12.Repository.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;

namespace project.B12.Repository.Repository
{
 
    public class SIRRepository : SugarRepository<Table_SIR>, ISIRRepository
    {

        private IUnitOfWork UOW;
        public SIRRepository(IUnitOfWork pUOW) : base(pUOW)
        {
            UOW = pUOW;
        }

        /// <summary>
        /// 根据菌中文名称查询结果
        /// </summary>
        /// <returns></returns>
        public List<Table_SIR> QueryByCNName(int bacteriaId,int medicineId)
        {
            List<Table_SIR> result;
            try
            {
                UOW.BeginTran();
                result = Query(x => x.BacteriaLevelID == bacteriaId && x.MedicineID == medicineId).Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }

        public List<Table_SIR> QueryByMedicineId(int medicineId)
        {
            List<Table_SIR> result;
            try
            {
                UOW.BeginTran();
                result = Query(o=>o.MedicineID==medicineId).Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }

        public List<Table_SIR> QueryAll()
        {
            List<Table_SIR> result;
            try
            {
                UOW.BeginTran();
                result = Query().Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }

        public void UpdateSIR(Table_SIR sir)
        {
            try
            {
                UOW.BeginTran();
                Update(sir);
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
        }

        public void DeleteSIR(Table_SIR sir)
        {
            try
            {
                UOW.BeginTran();
                Delete(sir);
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
        }

        public void AddSIR(Table_SIR sir)
        {
            try
            {
                UOW.BeginTran();
                Add(sir);
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
        }
    }
}
